
import React from 'react';
import ReactDOM from 'react-dom';
import "./login.css";
class Login extends React.Component {
    componentDidMount(){
       function getUuid() {
        return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
            return v.toString(16);
        });
        }
        /*var baseUrl = "http://192.168.2.169:8080/";*/   
        var baseUrl = "http://192.168.2.9:8080/learn/";
        var openUrl = "http://localhost:8116/question"
        var uuid = getUuid();
        var sid = "";
        $("#uuid").val(uuid);
        $("#validateImg").attr("src",baseUrl+"servlet/validateCodeServlet?uuid="+uuid);
        $(function () {
            /*ajax登录*/
            $("#ajaxLogin").click(function () {
                var url = baseUrl+"a/login";
                $.post(url, $("#loginFrom").serializeArray(), function (d) {
                    console.log(d);
                    var data = $.parseJSON(d);
                    if(data.success){
                        $("#sid").val(data.sessionId);//存session
                        window.location.href = openUrl
                    }else {
                        $(".warming").text(data.message);
                        $("#validateImg").attr("src","");
                        $("#validateImg").attr("src",baseUrl+"servlet/validateCodeServlet?uuid="+uuid+"&"+new Date().getTime());
                    }
                });
            });
            $(".change").click(function () {
                $("#validateImg").attr("src","");
                $("#validateImg").attr("src",baseUrl+"servlet/validateCodeServlet?uuid="+uuid+"&"+new Date().getTime());
            });
        });
    };
    render(){
        return(
            <div className="Login">
                <header className="header">在线学习平台</header>
                <div className="loginBody">
                    <div className="loginBox">
                        <p className="tab">
                            <b className="tabLogin">登录</b>
                            <b>注册</b>
                        </p>
                        <form className="loginOrEnroll" id="loginFrom">
                            <div className="divBox">
                                <span>账号：</span><input type="text" name="username" placeholder="请输入手机号"/>
                            </div>
                            <div className="divBox">
                                <span>密码：</span><input type="password" name="password" placeholder="请输入密码"/>
                            </div>
                            <div  className="remember">
                                <input type="checkbox" name="rememberMe"/>&nbsp;记住密码<a>忘记密码?</a>
                            </div>
                            <div className="divBox verification">
                                <span>验证：</span><input type="text"  name="validateCode" placeholder="请输入右侧验证码"/>
                            </div>
                            <img className="pic change" src="" id="validateImg"></img>
                            <em className="changePic change">看不清？换一张</em>
                            <p className="warming"></p>
                            <button type="button" className="loginBtn" id="ajaxLogin">登录</button>
                            <b className="code">二维码登录</b>
                            <input type="hidden" id="uuid" name="uuid"/>
                            <input type="hidden" name="mobileLogin" value="on"/>
                        </form>
                     </div>
                </div>
                <footer className="footer">企训星 &copy; 2016 沪ICP备14028521号-1</footer>
            </div>
        )
    }
}
export default Login;